package edu.sctu.jsoup.service;

import edu.sctu.jsoup.entity.CarEntity;
import edu.sctu.jsoup.repo.CarRepo;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class CarService {

    @Autowired
    private CarRepo carRepo;

    public void run(){
        try {
            Document document = Jsoup.connect(
                    "http://club.autohome.com.cn/#pvareaid=103419").get();
            Element element1 = document.select("forum-brand-box").first();
            Elements element2 = document.select("a");
            for (Element element :element2){
                String text = element.text();
                String url = element.attr("href");

                CarEntity carEntity = new CarEntity();

                carEntity.setUrl("http://club.autohome.com.cn/" + url);
                carEntity.setTitle(text);

                carRepo.save(carEntity);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        int maxId = (int) carRepo.count();
        System.out.println("表中最大的ID是："+maxId);
    }
}
